package com.komorebishao.crm.service.impl;

import com.komorebishao.crm.dao.CrmUserMapper;
import com.komorebishao.crm.dto.LoginResult;
import com.komorebishao.crm.pojo.CrmUser;
import com.komorebishao.crm.pojo.CrmUserExample;
import com.komorebishao.crm.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.swing.*;
import java.util.List;

/**
 * @Author: KomorebiShao
 * @Description:
 * @Date: Created in 2020-07-03
 * @Modified By:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    CrmUserMapper userMapper;
    public CrmUser getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(Long.valueOf(id));
    }

    public LoginResult login(String usercode, String password) {
        LoginResult result = new LoginResult();
        if (StringUtils.isNotEmpty(usercode) && StringUtils.isNotEmpty(password)){
            //根据账号查询
            CrmUserExample crmUserExample = new CrmUserExample();
            CrmUserExample.Criteria criteria = crmUserExample.createCriteria();
            criteria.andUserCodeEqualTo(usercode);
            List<CrmUser> crmUsers = userMapper.selectByExample(crmUserExample);
            if (crmUsers.size() > 0){
                //再判断密码是否相同
                if (DigestUtils.md5Digest(password.getBytes()).equals(crmUsers.get(0).getUserPassword())){
                    result.setUser(crmUsers.get(0));
                }else {
                    result.setMsg("密码不匹配！");
                }
            }else {
                result.setMsg("账号不存在");
            }
        }else {
            result.setMsg("账号或密码不能为空");
        }

        return result;
    }
}
